﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace HelpDesk
{
    public partial class BusquedaIncidencia : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnPalabra_Click(object sender, EventArgs e)
        {
            QueryBuilder Consulta = new QueryBuilder();
            DataTable GetData = new DataTable();
            Utilitario Utilidades = new Utilitario();
            string sql;

            if (txtPalabra.Text != "")
            {
                if(!txtPalabra.Text.Contains(' '))
                {
                    sql = "SELECT TICKET.ID_TICKET Ticket, TICKET.USUARIO_TICKET Solicitante, " +
                            "TICKET.TICKET_DESC Descripcion, to_char(TICKET.FECHA_INICIO) AS Fecha, INFRAESTRUCTURA.DESCRIPCION AS Infraestructura, " +
                            "PROBLEMA.DESCRIPCION AS Problema, AREA_TRABAJO.DESCRIPCION AS Area " +
                            "FROM TICKET INNER JOIN INFRAESTRUCTURA ON TICKET.INFRAESTRUCTURA = INFRAESTRUCTURA.ID_INFRAESTRUCTURA " +
                            "INNER JOIN PROBLEMA ON TICKET.PROBLEMA = PROBLEMA.ID_PROBLEMA " +
                            "INNER JOIN AREA_TRABAJO ON TICKET.AREA_ORIGEN = AREA_TRABAJO.ID_AREA " +
                            "WHERE ((TICKET.TICKET_DESC LIKE '%" + txtPalabra.Text + "%') OR (INFRAESTRUCTURA.DESCRIPCION LIKE '%" + txtPalabra.Text.ToUpper() + "%') OR (PROBLEMA.DESCRIPCION LIKE '%" + txtPalabra.Text + "%')) ORDER BY ID_TICKET";
                    GetData = Consulta.EjecutarQuery(sql);

                    if (GetData.Rows.Count != 0)
                    {
                        GridView1.DataSource = GetData;
                        GridView1.DataBind();
                    }
                }
                else
                {
                    Response.Write("<script language=javascript>alert('Por favor ingrese solo una palabra a buscar!');</script>");
                }
            }

            else
            {
                Response.Write("<script language=javascript>alert('Por favor ingrese una palabra a buscar!');</script>");
            }
        }

        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            Response.Redirect("DescripcionTIcket.aspx?TID=" + GridView1.SelectedValue.ToString());
        }

        protected void ddl_busqueda_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                QueryBuilder Fill_Ops = new QueryBuilder();
                string query = "Select * from " + ddl_busqueda.SelectedValue.ToString();
                switch (ddl_busqueda.SelectedValue.ToString())
                {
                    case "infraestructura":
                        ddl_opcion.DataValueField = "ID_INFRAESTRUCTURA";
                        break;
                    case "problema":
                        ddl_opcion.DataValueField = "id_problema";
                        break;
                    case "area_trabajo":
                        ddl_opcion.DataValueField = "id_area";
                        break;
                }

                ddl_opcion.DataTextField = "descripcion";
                ddl_opcion.DataSource = Fill_Ops.EjecutarQuery(query);
                ddl_opcion.DataBind();
                ddl_opcion.Items.Add("Seleccione una opcion");
                ddl_opcion.SelectedIndex = ddl_opcion.Items.Count;
            }
            catch
            { }
        }

        protected void btn_buscarop_Click(object sender, EventArgs e)
        {
            QueryBuilder Consulta = new QueryBuilder();
            DataTable GetData = new DataTable();

            string sql = "SELECT TICKET.ID_TICKET Ticket, TICKET.USUARIO_TICKET Solicitante, " +
                "TICKET.TICKET_DESC Descripcion, to_char(TICKET.FECHA_INICIO) AS Fecha, INFRAESTRUCTURA.DESCRIPCION AS Infraestructura, " +
                "PROBLEMA.DESCRIPCION AS Problema, AREA_TRABAJO.DESCRIPCION AS Area " +
                "FROM TICKET INNER JOIN INFRAESTRUCTURA ON TICKET.INFRAESTRUCTURA = INFRAESTRUCTURA.ID_INFRAESTRUCTURA " +
                "INNER JOIN PROBLEMA ON TICKET.PROBLEMA = PROBLEMA.ID_PROBLEMA " +
                "INNER JOIN AREA_TRABAJO ON TICKET.AREA_ORIGEN = AREA_TRABAJO.ID_AREA " +
                "WHERE TICKET." + ddl_busqueda.SelectedValue.ToString() + " = " + ddl_opcion.SelectedValue.ToString() + " ORDER BY ID_TICKET";
            GetData = Consulta.EjecutarQuery(sql);

            if (GetData.Rows.Count != 0)
            {
                GridView1.DataSource = GetData;
                GridView1.DataBind();
            }
        }
    }
}
